-- UIBuildingView5
-- Created by dengc Aug/13/2016
-- 建筑查看界面（按钮纵向按顺序排列，数量，位置自适应）

UIBuildingView5 = class("UIBuildingView5", function()
    return cc.Layer:create();
end);

function UIBuildingView5.create(gridData, okFuncList, okTextList, title, dialog, iconPath, drawIconFunc, offsetList, para)
    return UIBuildingView5.new(gridData, okFuncList, okTextList, title, dialog, iconPath, drawIconFunc, offsetList, para);
end

-- 构造函数
function UIBuildingView5:ctor(gridData, okFuncList, okTextList, title, dialog, iconPath, drawIconFunc, offsetList, para)
    -- 初始化
    self:setName("UIBuildingView5");
    local node = cc.CSLoader:createNode("layout/dungeon/BuildingView7.csb");
    self:addChild(node);
    self.node = node;

    self.CT = findChildByName(self.node, "CT");

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    assert(type(gridData) == "table", "必须传入格子数据");

    self.gridData  = gridData;

    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class) or {};

    local cfgOffsetY = buildingInfo["icon_offset_y"];
    if type(cfgOffsetY) ~= "number" then
        cfgOffsetY = 0;
    end

    self.title         = title or buildingInfo["title"];                               -- 标题
    self.iconPath      = iconPath or getLevelItemIconPath(buildingInfo["icon"]);       -- 图标路径
    self.dialog        = dialog or BuildingBonusM.getBuildingDialog(self.gridData);    -- 对话内容
    self.iconOffsetY   = cfgOffsetY;                                                   -- 图标纵向偏移量
    self.okFuncList    = okFuncList;
    self.okTextList    = okTextList;
    self.drawIconFunc  = drawIconFunc;
    self.offsetList    = offsetList;
    self.para          = para or {};

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 重绘
    self:redraw();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 适配
    self:resize();
end

-- 适配
function UIBuildingView5:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIBuildingView5:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIBuildingView5 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIBuildingView5:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBuildingView5");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册4个按钮点击事件
    for i = 1, #self.buttonList do
        local btn = findChildByName(self.CT, string.format("option%d", i));
        local text     = self.okTextList[i];
        local callback = self.okFuncList[i];

        TextStyleM.setTextStyle(btn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        btn:setTitleText(text);

        local function onButtonClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_select");
                if type(callback) == 'function' then
                    callback(i);
                end

                -- 关闭当前界面
                UIMgr.getCurrentScene():removeFormByName("UIBuildingView5");
            end
        end
        AddTouchEventListener(btn, onButtonClick);
    end
end

-- 重绘
function UIBuildingView5:redraw()
    local descLabel  = findChildByName(self.CT, "desc");
    local iconImg    = findChildByName(self.CT, "icon");
    local titleNode  = findChildByName(self.CT, "title_node");
    local titleLabel = findChildByName(titleNode, "title");

    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    -- 图标
    if self.drawIconFunc then
        self.iconNode = self:drawIconFunc(iconImg);
    else
        iconImg:loadTexture(self.iconPath);
        iconImg:setPositionY(iconImg:getPositionY() + self.iconOffsetY);
        iconImg:setScale(1.3);
    end

    if self.para.hide_icon then
        iconImg:setVisible(false);
    end

    -- 标题
    titleLabel:setString(self.title);
    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);

    -- 描述
    descLabel = applyString(descLabel, self.dialog);

    -- 绘制确定按钮
    self.buttonList = {};
    local optionBtn = findChildByName(self.CT, "option");
    for i = 1, #self.okFuncList do
        -- 克隆
        local button = optionBtn:clone();
        self.CT:addChild(button);
        button:setName("option" .. i);

        if self.para["long_btn_style"] then
            -- 长按钮样式
            button:loadTextureNormal(getButtonIconPath("btn_long2_yellow1"));
            button:loadTexturePressed(getButtonIconPath("btn_long2_yellow2"));
            button:loadTextureDisabled(getButtonIconPath("btn_long2_disable"));
        end

        -- 如果是置灰的
        local disable_list = self.para.disable_list or {};
        local enabled = table.indexOf(disable_list, i) < 0;
        button:setTouchEnabled(enabled);
        button:setBright(enabled);

        table.insert(self.buttonList, button);
    end

    optionBtn:setVisible(false);

    -- 自适应所有组件
    local bg = findChildByName(self.CT, "bg1");
    local iconNode = self.iconNode or iconImg;

    local nodeArr = table.append({titleNode, descLabel, iconNode, }, self.buttonList);

    local offsetList = self.offsetList or {30, 10, -10, -10, -10};

    -- 自适应
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL, nodeArr, offsetList);

    -- 调整标题的位置
    titleNode:setPositionY(titleNode:getPositionY() + 15);
end
